function values=parse_datalogger_logfile(filename)
% DATALOGGER - Process logfile and returns structured data.
% Usage:
%   values=parse_datalogger_logfile('2010_04_29_no00.txt');
%


%-----------------------
%f = fullfile('C:', 'Applications', 'matlab', 'myfun.m')
%data = textread('data.csv', '', 'delimiter', ',', ...     'emptyvalue', NaN);
%-----------------------

%% Log file specification

%---------------Log file structure---------
% No serie:  08932450
% Tempeture recording
% 
%                                   U
%                                   V
% 
% 19/04/2010 8:19:00            0.002
% 19/04/2010 8:19:10            0.002
% 
% 28/04/2010 16:57:50           0.803
% 28/04/2010 16:58:00           0.797
% ...
%---------------data specification---------
%dd/mm/yyyy HH:MM:SS "11 spaces" 0.000


data = textread(filename, '%s', 'delimiter', ' ');

metadata.filename=filename;
metadata.device_id='datalogger';
metadata.columns={'timestamp', 'V'};

datamatrix =[];
nitems = length(data);
%nitems = 100;

data_index = 1;
% Find the first data line
for i=1:nitems
    token = data(i);
    if strcmp(token,'U') == 1
        if strcmp(data(i+1),'V') == 1
            data_index =i+2;
%             data(data_index-1)
%             data(data_index)
%             data(data_index+1)
%             data(data_index+2)
            break
        end
    end
end


i = data_index; %
data_row = [];
while (i < nitems)
    %disp(i);
    token = data(i);
    %disp([token, i]);
    % date time reading
    [n, sensor_data] = parse_timestamp(data,i);
    data_row = new_data_row(sensor_data.timestamp);
    %data_row.date = sensor_data.date;
    %data_row.time = sensor_data.time;
    % voltage reading
    [n, sensor_data] = parse_voltage(data,n);
    data_row.V = sensor_data.V;
    i = n+1;
    datamatrix = [datamatrix; data_row2vector(data_row)];
    %disp(data_row);
end
metadata.start_date=datamatrix (1,1);
metadata.end_date=datamatrix(size(datamatrix,1),1);

%% output values
values.metadata=metadata;
values.data=datamatrix;

function vector = data_row2vector(data_row)
vector = [data_row.timestamp, data_row.V];

function data_row=new_data_row(time_stamp)
data_row.timestamp=time_stamp;
data_row.V=NaN;

function [next_index, sensor_data] = parse_timestamp(data, index)
%'19/04/2010'
sensor_data.date=char(data(index));

time=char(data(index+1));
if (length(time) ==7 )
    time = strcat('0', time);
end
sensor_data.time = time;

date_token = strcat(sensor_data.date, '_', sensor_data.time);
sensor_data.timestamp = datenum( date_token,'dd/mm/yyyy_HH:MM:SS');
next_index = index + 2;

function [next_index, sensor_data] = parse_voltage(data, index)
%'0.345'
sensor_data.V = str2double(data (index));
next_index = index;


%% output structured data
% 1st row => metadata
% following rows => data
%
% metadata: logfile, start_date, end_date,
%
% data: timestamp, humidity, temperature, NOx, COx, noise1, noise2, gps1,
% ..., gps10, battery
%

